﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame.GamePlay;

namespace MainGame.Menu
{
  /// <summary>
  /// Mostriamo lo score di fine livello
  /// </summary>
  public class LevelRecapScreen : DrawableGameComponent
  {
    Score displayed_score;
    bool result;
      Texture2D EndGameScreen;
    
      public LevelRecapScreen(Game game, Score score)
        : base(game)
    {
        displayed_score = score;
        

    }

    public override void Initialize()
    {
      Game.GetInputManager().OnTap += new InputManager.Tap(LevelRecapScreen_OnTap);

      base.Initialize();
    }

    void LevelRecapScreen_OnTap(Vector2 tap_position)
    {
      if (tap_position.ToPoint(screen_transform).X > 400)
      {
        if (OnContinue != null) OnContinue();
      }
      else
      {
        if (OnExit != null) OnExit();
      }
             

    }

    SpriteBatch spriteBatch;
    SpriteFont large_font;
    Matrix screen_transform;
    protected override void LoadContent()
    {
      var content = Game.Content;

      spriteBatch = Game.GetContext().SpriteBatch;
      screen_transform = Game.GetContext().ScreenTransform;
      EndGameScreen = content.Load<Texture2D>(@"Images\\Menu\\sfondoMenu");
      large_font = content.Load<SpriteFont>(@"large_font");
      
      base.LoadContent();
    }

    private bool BestScore()
    {

        int playerScore = 0, bestScore = 0;

        for (int i = 0; i < displayed_score.PlayerScores.Count; i++)
        {
            var ps = displayed_score.PlayerScores[i];

            if (ps.Name.Equals("Giuseppe"))
                playerScore = ps.Score;


            if (bestScore < ps.Score)
                bestScore = ps.Score;
        }

        if (playerScore >= bestScore)
            return true;
        else
            return false;

    }

    public override void Draw(GameTime gameTime)
    {
        var content = Game.Content;
        spriteBatch.Begin(screen_transform);

        result = BestScore();

      
            spriteBatch.Draw(EndGameScreen, new Vector2(0, 0), null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
        if (result)
            spriteBatch.DrawString(content.Load<SpriteFont>("huge_font"), @"Hai vinto!!", new Vector2(130, 290), Color.Blue);
        else
            spriteBatch.DrawString(content.Load<SpriteFont>("huge_font"), @"Hai perso!!", new Vector2(130, 290), Color.Red);



        for (int i = 0; i < displayed_score.PlayerScores.Count; i++)
        {
            var ps = displayed_score.PlayerScores[i];
            spriteBatch.DrawString(large_font, ps.Name + " = " + ps.Score.ToString(), new Vector2(50, 50 + i * 50),
              Microsoft.Xna.Framework.Color.Yellow);
        }

        

        spriteBatch.DrawString(large_font, "Exit", new Vector2(50, 400), Microsoft.Xna.Framework.Color.Yellow);
        spriteBatch.DrawString(large_font, "Continue", new Vector2(600, 400), Microsoft.Xna.Framework.Color.Yellow);

      spriteBatch.End();

      base.Draw(gameTime);
    }

    public event MenuEvent OnExit;
    public event MenuEvent OnContinue;
  }
}
